package com.zhang.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.system.dto.HomeAdvSearchDTO;
import com.zhang.system.entity.HomeAdv;
import com.zhang.system.mapper.HomeAdvMapper;
import com.zhang.system.service.IHomeAdvService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 首页轮播广告 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2024-06-05
 */
@Service
public class HomeAdvServiceImpl extends ServiceImpl<HomeAdvMapper, HomeAdv> implements IHomeAdvService {

    @Override
    public Page<HomeAdv> getHomeAdvList(HomeAdvSearchDTO homeAdvSearchDTO) {
        LambdaQueryWrapper<HomeAdv> wrapper = new LambdaQueryWrapper<>();
//        拼接动态sql
        wrapper.or()
                .like(!StrUtil.isBlank(homeAdvSearchDTO.getKeyWord()), HomeAdv::getName, homeAdvSearchDTO.getKeyWord())
                .or().eq(!StrUtil.isBlank(homeAdvSearchDTO.getKeyWord()), HomeAdv::getId, homeAdvSearchDTO.getKeyWord())
//               如果具有查询全部的权限，则可将此字段纳入搜索字段中
                .or().eq(!StrUtil.isBlank(homeAdvSearchDTO.getKeyWord()) && StpUtil.hasPermission("pass-homeAdv"), HomeAdv::getPublisherId, homeAdvSearchDTO.getKeyWord())
//               如果不具备查询所有的权限，则只能查询与自己相关的数据
                .eq(!StpUtil.hasPermission("pass-homeAdv"), HomeAdv::getPublisherId, StpUtil.getLoginIdAsLong())
                .eq(homeAdvSearchDTO.getState() != -1, HomeAdv::getStatus, homeAdvSearchDTO.getState());
        return page(new Page<>(homeAdvSearchDTO.getPageNum(), homeAdvSearchDTO.getPageSize()), wrapper);
    }

    @Override
    public List<HomeAdv> getUsingHomeAdvList() {
        return this.baseMapper.getUsingHomeAdvList();
    }
}
